package com.jeff.android.lab.database.ormlite;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;
import com.jeff.android.lab.database.ormlite.model.UserInfo;
import com.jeff.utils.ToastUtil;

import java.sql.SQLException;
import java.util.List;

import butterknife.OnClick;

public class OrmLiteFragment extends BaseFragment {

    @Override
    protected int getRootLayout() {
        return R.layout.db_fragment_ormlite;
    }

    @OnClick(R.id.bt_create)
    public void create() {
        UserInfo userInfo = new UserInfo("dingfeng", 29, "本科");
        try {
            DataBaseHelper.getDatabaseHelper(mContext).getUserDao()
                    .create(userInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.bt_query)
    public void query() {
        try {
            List<UserInfo> users = DataBaseHelper.getDatabaseHelper(mContext).getUserDao()
                    .queryForAll();
            if (users != null) {
                ToastUtil.show(mContext, users.get(0).toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
